package com.whf.android.jar.web;

import android.annotation.SuppressLint;
import android.os.Build;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.whf.android.jar.app.Latte;
import com.whf.android.jar.web.news.NewsWebView;

/**
 * WebView 的属性设置
 *
 * @author hf
 * @version 1.0.2
 */
public class WebViewInitializer {

    @SuppressLint({"SetJavaScriptEnabled", "ObsoleteSdkInt"})
    public NewsWebView createWebView(NewsWebView webView) {

        //设置Web内容调试已启用
        WebView.setWebContentsDebuggingEnabled(Latte.getDebug());

        //支持获取手势焦点，输入用户名、密码或其他
        webView.requestFocusFromTouch();

        //cookie
        final CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            cookieManager.setAcceptThirdPartyCookies(webView, true);
        }
        CookieManager.setAcceptFileSchemeCookies(true);

        //不能横向滚动
        webView.setHorizontalScrollBarEnabled(false);
        //不能纵向滚动
        webView.setVerticalScrollBarEnabled(false);
        //允许截图
        webView.setDrawingCacheEnabled(true);
        //屏蔽长按事件
        webView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
        //初始化WebSettings
        final WebSettings settings = webView.getSettings();

        settings.setUserAgentString("latte");

        //支持js
        settings.setJavaScriptEnabled(true);
        //支持插件
//        settings.setPluginsEnabled(true);

        //提高渲染的优先级
//        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);

        final String ua = settings.getUserAgentString();
        settings.setUserAgentString(ua + "Latte");
        //隐藏缩放控件
        settings.setBuiltInZoomControls(false);
        settings.setDisplayZoomControls(false);
        //禁止缩放
        settings.setSupportZoom(false);
        //文件权限
        settings.setAllowFileAccess(true);
        settings.setAllowContentAccess(true);

        //WebView 跨域访问漏洞
        settings.setAllowFileAccessFromFileURLs(false);
        settings.setAllowUniversalAccessFromFileURLs(false);

        //缓存相关
        settings.setAppCacheEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);//数据库缓存
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);//设置缓存模式

        //启用地理定位
        settings.setGeolocationEnabled(true);
        //设置可以访问文件
        settings.setAllowFileAccess(true);
        //当webView调用requestFocus时为webView设置节点
        settings.setNeedInitialFocus(true);
        //支持通过JS打开新窗口
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        //支持自动加载图片
        settings.setLoadsImagesAutomatically(true);

        //设置编码格式
        settings.setDefaultTextEncodingName("utf-8");
        //设置 WebView 的字体，默认字体为 "sans-serif"
        settings.setStandardFontFamily("sans-serif");
        //设置 WebView 字体的大小，默认大小为 16
        settings.setDefaultFontSize(20);
        //设置 WebView 支持的最小字体大小，默认为 8
        settings.setMinimumFontSize(12);

        return webView;
    }
}
